package com.example.smartcity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.smartcity.ui.HomeActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 登录
 * @author YSJ
 * @date 2021/4/17 : 14:36
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViewById(R.id.login).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        EditText editUserName=findViewById(R.id.userName);
        String userName = editUserName.getText().toString();

        EditText editPassword=findViewById(R.id.password);
        String password = editPassword.getText().toString();

        if (userName.equals("")||password.equals("")){
            Toast.makeText(LoginActivity.this,
                    "密码或者账号不能为空！",Toast.LENGTH_SHORT).show();
            return;
        }

        OkHttpClient client= new OkHttpClient();
        //请求体
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");

        JSONObject jsonObject=new JSONObject();

        try {
            jsonObject.put("password",password);
            jsonObject.put("username",userName);


            String json=jsonObject.toString();
            RequestBody body=RequestBody.create(json,mediaType);
            Request request=new Request.Builder()
                    .post(body)
                    .url("http://124.93.196.45:10002/login")
                    .build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(LoginActivity.this
                            ,e.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    String string = response.body().string();
                    try {
                        JSONObject jsonObject1=new JSONObject(string);
                        if ((int)jsonObject1.opt("code")==200){
                            //登录成功
                            String token = (String) jsonObject1.opt("token");
                            SharedPreferences sp=LoginActivity.this.getSharedPreferences("app",0);
                            SharedPreferences.Editor editor=sp.edit();
                            editor.putString("token",token);
                            editor.commit();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                                    finish();
                                }
                            });

                        }else {
                            //登录失败
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(LoginActivity.this,
                                            "账号或者密码错误！",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}